//package wondersgroup.hsswidget.slidingcard;
//
//import android.support.design.widget.CoordinatorLayout;
//import android.support.v4.view.ViewCompat;
//import android.view.View;
//
///**
// * @author 黄赛赛
// * @version V1.0
// * @Title: ${file_name}
// * @Package ${package_name}
// * @Description: ${todo}()
// * @date 2016/10/19 14:05
// */
//public class SlidingCardBehavior extends CoordinatorLayout.Behavior<SlidingCardLayout>{
//
//    int mInitialOffset = 0;
//
//    @Override
//    public boolean onMeasureChild(CoordinatorLayout parent, SlidingCardLayout child, int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec, int heightUsed) {
////        return super.onMeasureChild(parent, child, parentWidthMeasureSpec, widthUsed, parentHeightMeasureSpec, heightUsed);
//        //测绘子控件
//        //3 headerviewheight
//        int offset  = getChildMeasureOffset(parent, child);
//        int mersuredHeight = View.MeasureSpec.getSize(parentHeightMeasureSpec) - heightUsed - offset;
//        int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(mersuredHeight,View.MeasureSpec.EXACTLY);
//        child.measure(parentWidthMeasureSpec,heightMeasureSpec);
//        return true;
//    }
//
//    private int getChildMeasureOffset(CoordinatorLayout parent, SlidingCardLayout child) {
//        int offset = 0;
//        for (int i =0, size = parent.getChildCount(); i < size; i++){
//            View view = parent.getChildAt(i);
//            //除开自己
//            if (view != child && view instanceof SlidingCardLayout){
//                offset += ((SlidingCardLayout) view).getmHeaderViewHeight();
//
//            }
//        }
//        return offset;
//    }
//
//
//
//    @Override
//    public boolean onLayoutChild(CoordinatorLayout parent, SlidingCardLayout child, int layoutDirection) {
//        parent.onLayoutChild(child,layoutDirection);
//        //int offset = preview.getTop + preview.getHeaderHeight();
//        //控制view离顶部的距离
//        SlidingCardLayout preView =  getPreViewsChild(parent, child);
//        if (preView != null){
//            int offset = preView.getTop() +  preView.getmHeaderViewHeight();
//            child.offsetTopAndBottom(offset);
//        }
//        mInitialOffset = child.getTop();
//        return true;
//    }
//
//    private SlidingCardLayout getPreViewsChild(CoordinatorLayout parent, SlidingCardLayout child) {
//
//        int cardIndex = parent.indexOfChild(child);
//        for (int i = cardIndex-1; i >= 0 ; i--){
//                View view = parent.getChildAt(i);
//                if (view instanceof  SlidingCardLayout){
//                    return (SlidingCardLayout) view;
//                }
//        }
//        return null;
//    }
//
//    private SlidingCardLayout getNextViewsChild(CoordinatorLayout parent, SlidingCardLayout child) {
//
//        int cardIndex = parent.indexOfChild(child);
//        for (int i = cardIndex+1,size = parent.getChildCount(); i < size  ; i++){
//            View view = parent.getChildAt(i);
//            if (view instanceof  SlidingCardLayout){
//                return (SlidingCardLayout) view;
//            }
//        }
//        return null;
//    }
//
//    @Override
//    public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, SlidingCardLayout child, View directTargetChild, View target, int nestedScrollAxes) {
//        //boolean 是否需要检讨滑动  水平还是垂直
//        boolean isVertical = (nestedScrollAxes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0;
////        return super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, nestedScrollAxes);
//        return  isVertical && child == directTargetChild;
//    }
//
//    @Override
//    public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, SlidingCardLayout child, View target, int dx, int dy, int[] consumed) {
//
//        if (child.getTop() > mInitialOffset){
//            int shift = scroll(child, dy, mInitialOffset, mInitialOffset+child.getHeight() - child.getmHeaderViewHeight() );
//
//        }
//        super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed);
//    }
//
//    @Override
//    public void onNestedScroll(CoordinatorLayout coordinatorLayout, SlidingCardLayout child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
//        super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);
//
//        int shift = scroll(child, dyUnconsumed, mInitialOffset, mInitialOffset+child.getHeight() - child.getmHeaderViewHeight() );
//    }
//
//    private int scroll(SlidingCardLayout child, int dy, int minOffset, int maxOffset) {
//        //a 在此范围里面[minOffset maxOffset]
//        final int initialOffset = child.getTop();
//        int delta = clamp(initialOffset - dy, minOffset,maxOffset) - initialOffset;
//        return  delta;
//    }
//
//    private int clamp(int i, int minOffset, int maxOffset) {
//        if (  i <  minOffset){
//            return minOffset;
//        }else if ( i > maxOffset){
//            return maxOffset;
//        }else {
//            return i;
//        }
//    }
//}
